// pages/culture/topic/topic.js
const app = getApp()
const time = require('../../../utils/util.js')
Page({
  data: {
    tid: '',
    topic: [],
    remarkList: [],
    InputBottom: 0,
    content: '',
    openid: '',
    avatar: '',
    nickname: '',
    isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const ui = wx.getStorageSync('userInfo')
    this.setData({
      openid: ui.openid,
      avatar: ui.avatarUrl,
      nickname: ui.nickName,
      tid: options.id,
      loadModal: true
    })
    // console.log(ui)
    // this.setData({
    //   tid: options.id,
    //   loadModal: true
    // })
    wx.cloud.callFunction({
      name: 'getTopic',
      data: {
        id: options.id
      }
    }).then(res => {
      console.log(res)
      if(res.result.list[0].remarkList.length != 0){
        for (let i = 0; i < res.result.list[0].remarkList.length; i++) {
          res.result.list[0].remarkList[i]._createTime = time.formatTimeTwo(res.result.list[0].remarkList[i]._createTime, 'Y/M/D h:m:s')
        }
      }
      res.result.list[0]._createTime = time.formatTimeTwo(res.result.list[0]._createTime, 'Y/M/D h:m:s')
      this.setData({
        topic: res.result.list[0],
        remarkList: res.result.list[0].remarkList,
        loadModal: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.cloud.callFunction({
    //   name: 'getTopic',
    //   data: {
    //     id: this.data.tid
    //   }
    // }).then(res => {
    //   console.log(res)
    //   for (let i = 0; i < res.result.list[0].remarkList.length; i++) {
    //     res.result.list[0].remarkList[i]._createTime = time.formatTimeTwo(res.result.list[0].remarkList[i]._createTime, 'Y/M/D h:m:s')
    //   }
    //   res.result.list[0]._createTime = time.formatTimeTwo(res.result.list[0]._createTime, 'Y/M/D h:m:s')
    //   this.setData({
    //     topic: res.result.list[0],
    //     remarkList: res.result.list[0].remarkList
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  inputRemark(e) {
    this.setData({
      content: e.detail.value
    })
  },
  insertRemark: function () {
    const _this = this
    _this.setData({
      loadModal: true
    })
    wx.cloud.callFunction({
      name: 'insertRemark',
      data: {
        openid: _this.data.openid,
        avatar: _this.data.avatar,
        nickname: _this.data.nickname,
        tid: _this.data.tid,
        content: _this.data.content,
        _createTime: new Date().getTime(),
        _updateTime: new Date().getTime(),
      }
    }).then(function (res) {

      wx.showToast({
        title: '发表评论成功',
      })
      wx.cloud.callFunction({
        name: 'getAllRemark',
        data: {
          id: _this.data.tid
        }
      }).then(res => {
        for (let i = 0; i < res.result.data.length; i++) {
          res.result.data[i]._createTime = time.formatTimeTwo(res.result.data[i]._createTime, 'Y/M/D h:m:s')
        }
        _this.setData({
          content: '',
          remarkList: res.result.data,
          loadModal: false
          
        })
      })
    })
  }
})